package com.ziyia.a2_5_7;

import androidx.annotation.NonNull;
import androidx.annotation.StringDef;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.Context;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private MyAdapter myAdapter;
    private List<Map<String, String>> maps;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView recyclerView = findViewById(R.id.list);

        maps = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("title", "title - " + i);
            map.put("text", "text - " + i);
            maps.add(map);
        }


//        线性布局
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

//         风格布局
//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
//        recyclerView.setLayoutManager(gridLayoutManager);

//         瀑布布局
//        StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayout.VERTICAL);
//        recyclerView.setLayoutManager(staggeredGridLayoutManager);




        // 设置 Adapter
        myAdapter = new MyAdapter(this, maps);
        recyclerView.setAdapter(myAdapter);



    }

    public void test(View view) {
        Map<String, String> map = maps.get(0);
        map.put("title", "update");
        myAdapter.notifyItemChanged(0);
    }


    static class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {



        private Context context;
        private List<Map<String, String>> maps;

        public MyAdapter(Context context, List<Map<String, String>> maps) {
            this.context = context;
            this.maps = maps;
        }



        class MyHolder extends RecyclerView.ViewHolder {
            TextView title;
            TextView text;

            public MyHolder(@NonNull View itemView) {
                super(itemView);
                title = itemView.findViewById(R.id.title);
                text = itemView.findViewById(R.id.text);
            }
        }


        @NonNull
        @Override
        public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.item, null);
            MyHolder holder = new MyHolder(view);
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyHolder holder, int position) {
            Map<String, String> data = maps.get(position);
            String title = data.get("title");
            String text = data.get("text");

            holder.title.setText(title);
            holder.text.setText(text);
            
            holder.title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(context, title, Toast.LENGTH_SHORT).show();
                }
            });
        }

        @Override
        public int getItemCount() {
            return maps.size();
        }
    }
}